package com.ciban.smarthome.common.framework.fund;

import com.ciban.smarthome.R;
import com.ciban.smarthome.common.framework.fund.widget.GestureContentView;
import com.ciban.smarthome.common.framework.fund.widget.GestureDrawline.GestureCallBack;
import com.ciban.smarthome.common.framework.fund.widget.LockIndicator;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.Toast;

/**
 * 
 * 手势密码设置界面
 * 
 */
public class GestureEditActivity extends Activity implements OnClickListener {
	private LockIndicator mLockIndicator;
	private FrameLayout mGestureContainer;
	private GestureContentView mGestureContentView;
	private boolean mIsFirstInput = true;
	private String mFirstPassword = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gesture_edit);
		setUpViews();
	}

	private void setUpViews() {
		mLockIndicator = (LockIndicator) findViewById(R.id.lock_indicator);
		mGestureContainer = (FrameLayout) findViewById(R.id.gesture_container);

		// 初始化一个显示各个点的viewGroup
		mGestureContentView = new GestureContentView(this, false, "",
				new GestureCallBack() {
					@Override
					public void onGestureCodeInput(String inputCode) {
						if (!isInputPassValidate(inputCode)) {
							mGestureContentView.clearDrawlineState(0L);
							return;
						}
						if (mIsFirstInput) {
							mFirstPassword = inputCode;
							updateCodeList(inputCode);
							mGestureContentView.clearDrawlineState(0L);
						} else {
							if (inputCode.equals(mFirstPassword)) {
								Toast.makeText(GestureEditActivity.this,
										"设置成功", Toast.LENGTH_SHORT).show();
								mGestureContentView.clearDrawlineState(0L);
								GestureEditActivity.this.finish();
							} else {
								// 保持绘制的线，1.5秒后清除
								mGestureContentView.clearDrawlineState(1300L);
							}
						}
						mIsFirstInput = false;
					}

					@Override
					public void checkedSuccess() {

					}

					@Override
					public void checkedFail() {

					}
				});
		// 设置手势解锁显示到哪个布局里面
		mGestureContentView.setParentView(mGestureContainer);
		updateCodeList("");
	}

	private void updateCodeList(String inputCode) {
		// 更新选择的图案
		mLockIndicator.setPath(inputCode);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		default:
			break;
		}
	}

	private boolean isInputPassValidate(String inputPassword) {
		if (TextUtils.isEmpty(inputPassword) || inputPassword.length() < 4) {
			return false;
		}
		return true;
	}

}
